// pages/auth/accountLogin/accountlogin.js
const {
  isValPhone
} = require('../../../utils/check')
const {
  authlogin,
  authregister
} = require('../../../api/api')
const {
  login
} = require("../../../utils/util")
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ishidden: true,
    current: 1,
    codeText: '获取验证码',
    counting: false,
    logins: {
      username: "",
      password: ""
    }, //登录数据
    register: {
      username: '',
      password: "",
      confirmpwd: "", //确认密码
      mobile: "", //手机号
      code: "", //验证码
      wxCode: "", //
    } //注册数据
  },
  //双向绑定
  blurloginphon(e) {
    this.setData({
      "logins.username": e.detail.value
    })
  },
  blurloginpwd(e) {
    this.setData({
      "logins.password": e.detail.value
    })
  },
  blurregistername(e) {
    this.setData({
      "register.username": e.detail.value
    })
  },
  blurregistermobile(e) {
    this.setData({
      "register.mobile": e.detail.value
    })
  },
  blurregistercode(e) {
    this.setData({
      "register.code": e.detail.value
    })
  },
  blurregisterpwd(e) {
    this.setData({
      "register.password": e.detail.value
    })
  },
  blurconfirmpwd(e) {
    this.setData({
      "register.confirmpwd": e.detail.value
    })
    if (e.detail.value != this.data.register.password) {
      this.setData({
        ishidden: false
      })
    } else {
      this.setData({
        ishidden: true
      })
    }
  },

  //点击对登录注册
  submint() {
    if (this.data.ishidden == false) {
      return
    }
    //登录注册
    if (this.data.current == 1) {
      if (this.data.logins.password.length < 6 || this.data.logins.username.length < 6) {
        // 登录
        wx.showModal({
          title: '错误信息',
          content: '用户名和密码不得少于6位',
          showCancel: false
        });
        return false;
      }
      const data = {
        username: this.data.logins.username,
        password: this.data.logins.password
      }
      authlogin(data).then(res => {
        if (res.errno == 0) {
          console.log(app);
          app.globaData.hasLogin = true;
          wx.setStorageSync('userInfo', res.data.userInfo);
          wx.setStorage({
            key: "token",
            data: res.data.token,
            success: function () {
              wx.switchTab({
                url: '/pages/user/index/index'
              });
            }
          });
        } else {
          app.globaData.hasLogin = false;
          wx.showModal({
            title: '登陆失败',
            content: '用户名或密码错误',
            showCancel: false
          });
        }
      })

    } else {
      //注册
      if (this.data.register.password.length < 6 || this.data.register.username.length < 6) {
        wx.showModal({
          title: '错误信息',
          content: '用户名和密码不得少于6位',
          showCancel: false
        });
        return false;
      }
      if (this.data.register.password != this.data.register.confirmpwd) {
        wx.showModal({
          title: '错误信息',
          content: '两次密码不一致',
          showCancel: false
        });
        return false;
      }
      if (this.data.register.mobile.length == 0 || this.data.register.code.length == 0) {
        wx.showModal({
          title: '错误信息',
          content: '手机号和验证码不能为空',
          showCancel: false
        });
        return false;
      }
      if (!isValPhone(this.data.register.mobile)) {
        wx.showModal({
          title: '错误信息',
          content: '手机号输入不正确',
          showCancel: false
        });
        return false;
      }
      login().then(res => {
        let data = {
          username: this.data.register.username,
          password: this.data.register.password,
          mobile: this.data.register.mobile,
          code: this.data.register.code,
          wxCode: res.code
        }
        authregister(data).then(res => {
          console.log(res);
          if (res.errno == 0) {
            wx.setStorageSync('userInfo', res.data.userInfo)
            wx.setStorageSync("token", res.data.token, )
            wx.switchTab({
              url: '/pages/user/index/index',
            })
          } else {
            wx.showModal({
              title: '错误信息',
              content: res.data.errmsg,
              showCancel: false
            });
          }
        })
      })
    }
  },


  // 登陆注册监听
  click(e) {
    let index = e.currentTarget.dataset.code;
    this.setData({
      current: index
    })
  },
  //获取验证码 
  getCode() {
    var that = this;
    if (!that.data.counting) {
      wx.showToast({
        title: '验证码已发送',
      })
      //开始倒计时60秒
      that.countDown(that, 60);
    }
  },
  //倒计时60秒
  countDown(that, count) {
    if (count == 0) {
      that.setData({
        codeText: '获取验证码',
        counting: false
      })
      return;
    }
    that.setData({
      counting: true,
      codeText: count + '秒后重新获取',
    })
    setTimeout(function () {
      count--;
      that.countDown(that, count);
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})